//
//  CommissionSettlementController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/3/19.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CommissionSettlementController.h"
#import "CommissionSetCell.h"
#import "CommissionPayModeController.h"
#import "CommissionModeCell.h"

#import "CommissionBankModel.h"
#import "CommissionAlipayModel.h"

@interface CommissionSettlementController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *commissionSettlementView;

@property(nonatomic,strong)UITextField *strikeTextField;

@property(nonatomic,strong)UIButton *applyButton;

@property(nonatomic,copy)NSString *commissionNumStr;

@property(nonatomic,strong)MBProgressHUD *progressHUD;

@property(nonatomic,strong)CommissionAlipayModel *alipayModel;
@property(nonatomic,strong)CommissionBankModel *bankModel;
@property(nonatomic,copy)NSString *pay_type;

@end

@implementation CommissionSettlementController

static NSString *const CommissionSettlemenReuseIdentifier = @"CommissionSettlemenReuseIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self buildView];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.strikeTextField resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.strikeTextField.text = @"";
}

- (void)buildView
{
    self.title = @"佣金申请结算";
    [self.view addSubview:self.commissionSettlementView];

    [self commissionNumberRequeset];
}

- (void)commissionNumberRequeset
{
    //不显示null
    self.commissionNumStr = @"";
    @weakify(self);
    _progressHUD = [MBProgressHUD show:self.view andMessage:nil];
    [self.view addSubview:_progressHUD];
    
    EASY_DISPATCH_GLOBAL_QUEUE_DEFAULT(^{
        
        NSDictionary *params = @{@"username":USERNAME};
        
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Cash/info" completeBlock:^(id object) {
           
            NSLog(@"%@",object);
            if (JudgeBackDataCode) {
                @strongify(self);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    NSDictionary *dataDict = [object objectForKey:@"data"];
                    self.commissionNumStr = [dataDict objectForKey:@"current_commission"];
                    
                    [_progressHUD hide:true];
                    [self.commissionSettlementView reloadData];
                });
            }
        }];
    });
}


#pragma mark  点击申请
- (void)commissionApplyClick
{
    [self.strikeTextField resignFirstResponder];

    
    if (self.pay_type == nil) {
        self.progressHUD = [MBProgressHUD showMessage:@"未选择付款方式" toView:self.view dimBackground:false AndCompleteBlock:nil];
        [self.view addSubview:self.progressHUD];
        return;
    }
    
    NSDictionary *params = @{@"username":USERNAME,
                             @"commission_num":self.strikeTextField.text,
                             @"address_type":self.pay_type
                             };
    NSLog(@"%@",params);
     @weakify(self);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/expand/doCommissionCash" completeBlock:^(id object) {
        
       
        if (JudgeBackDataCode) {
            @strongify(self);
            self.progressHUD = [MBProgressHUD showMessage:@"佣金结算申请成功" toView:self.view dimBackground:false AndCompleteBlock:^(id object){
                
                [self.navigationController popToRootViewControllerAnimated:true];
            }];
            [self.view addSubview:self.progressHUD];
        }
    }];
    
}

- (void)strikeTextFieldChange
{
    
    _applyButton.enabled = self.strikeTextField.text.length > 0;
    
    if ([self.strikeTextField.text floatValue] > [self.commissionNumStr floatValue]) {
        _applyButton.enabled = false;
    }else{
        _applyButton.enabled = true;
    }
    
    
    if (_applyButton.enabled) {
        [_applyButton setBackgroundColor:DEFAULT_COLOR];
    }else{
        [_applyButton setBackgroundColor:[UIColor lightGrayColor]];
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        
        if (self.bankModel || self.alipayModel) {
            
            static NSString *identifier=@"bankModelbankModel";
            
            CommissionModeCell *cell= [tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (cell==nil) {
                cell=[[CommissionModeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
            }
            
            if (self.bankModel) {
                
                [cell.iconImg sd_setImageWithURL:[NSURL URLWithString:self.bankModel.bank_logo] placeholderImage:[UIImage imageNamed:@"icon_bank_default"]];
                cell.NameLabel.text = self.bankModel.bank_name;
                cell.numLabel.text = self.bankModel.card_no;
    
            }else{
                cell.iconImg.image = [UIImage imageNamed:@"icon_aliPay"];
                cell.NameLabel.text = self.alipayModel.alipay_realname;
                cell.numLabel.text = self.alipayModel.alipay_account;
                
            }
            return cell;
            
        }else{
            
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CommissionSettlemenReuseIdentifier];
            
            if (cell==nil) {
                cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CommissionSettlemenReuseIdentifier];
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

            cell.imageView.image = [UIImage imageNamed:@"icon_payMode_choose"];
            cell.textLabel.text = @"请选择结算方式";
            cell.textLabel.font = FONT(15);
            return cell;
        }
   
    }else{
        
        @weakify(self);
        CommissionSetCell *cell = [[CommissionSetCell alloc] initWithFrame:CGRectMake(0,0, WIDTH,145) commissionBalance:self.commissionNumStr strikeStr:^(UITextField *strikeTextField) {
            @strongify(self);
           
            self.strikeTextField = strikeTextField;
            [ [NSNotificationCenter defaultCenter]addObserver:self selector:@selector(strikeTextFieldChange)name:UITextFieldTextDidChangeNotification object:strikeTextField];

            
        } applyClick:^{
            
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        CommissionPayModeController *modeVC = [[CommissionPayModeController alloc] init];
        
        @weakify(self);
        modeVC.bankBlock = ^(CommissionBankModel *bankModel){
            @strongify(self);
            self.alipayModel = nil;
            self.pay_type = @"1";
            self.bankModel = bankModel;
            [self.strikeTextField becomeFirstResponder];
            [self.commissionSettlementView reloadData];
        };
        modeVC.alipayBlock = ^(CommissionAlipayModel *alipayModel){
            @strongify(self);
            self.bankModel = nil;
            self.pay_type = @"2";
            self.alipayModel = alipayModel;
            [self.strikeTextField becomeFirstResponder];
            [self.commissionSettlementView reloadData];
        };
        
        [self.navigationController pushForm:self.navigationController pushToAndHidden:modeVC];
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 ) {
        return 55;
    }
    return 145;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return 100;
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        
        UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0,0,WIDTH,100)];
        bottomView.backgroundColor = self.commissionSettlementView.backgroundColor;
        
        UILabel *label = [[UILabel alloc] init];
        [bottomView addSubview:label];
        label.textColor = DEFAULT_COLOR;
        label.font = FONT(11);
        label.text = @"* 申请三个工作日内到账,节假日顺延,确保绑定的支付宝和银行卡无误";
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bottomView.mas_left).offset(15);
            make.top.equalTo(bottomView.mas_top).offset(10);
            make.width.mas_equalTo(WIDTH-30);
        }];
        
        self.applyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bottomView addSubview:_applyButton];
        [_applyButton setTitle:@"立 即 申 请" forState:UIControlStateNormal];
        [_applyButton setBackgroundColor:[UIColor lightGrayColor]];
        
        _applyButton.enabled = false;
        _applyButton.layer.masksToBounds = true;
        _applyButton.layer.cornerRadius = 5;
        [_applyButton addTarget:self action:@selector(commissionApplyClick) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        [_applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(label.mas_width);
            make.height.mas_equalTo(40);
            make.top.equalTo(label.mas_bottom).offset(25);
            make.left.equalTo(label.mas_left);
        }];
        
        
        return bottomView;
    }
    return nil;
}


- (UITableView *)commissionSettlementView
{
    if (!_commissionSettlementView) {
        _commissionSettlementView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT) style:UITableViewStyleGrouped];
        _commissionSettlementView.delegate = self;
        _commissionSettlementView.dataSource = self;
    }
    return _commissionSettlementView;
}


- (UITextField *)strikeTextField
{
    if (!_strikeTextField) {
        _strikeTextField = [[UITextField alloc] init];
    }
    return  _strikeTextField;
}


@end
